package com.dony.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dony.common.utils.DateUtils;
import com.dony.common.utils.SecurityUtils;
import com.dony.order.domain.BlackRecord;
import com.dony.order.domain.Order;
import com.dony.order.mapper.BlackRecordMapper;
import com.dony.order.service.IBlackRecordService;
import com.dony.order.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 订单Service业务层处理
 *
 * @author dony
 * @date 2022-09-07
 */
@Slf4j
@Service("blackRecordService")
public class BlackRecordServiceImpl extends ServiceImpl<BlackRecordMapper, BlackRecord> implements IBlackRecordService {

    @Autowired
    IOrderService orderService;

    @Override
    public void blackOrder(String orderSn, Boolean black) {
        if (black) {
            Order order = orderService.getById(orderSn);
            BlackRecord record = new BlackRecord();
            record.setOrderSn(orderSn);
            record.setBuyer_user_id(order.getBuyerUserId());
            record.setBuyer_username(order.getBuyerUsername());
            record.setCreateBy(SecurityUtils.getUsername());
            record.setCreateTime(DateUtils.getNowDate());
            this.save(record);
        }else {
            QueryWrapper<BlackRecord> wrapper = new QueryWrapper<>();
            wrapper.eq("order_sn",orderSn);
            this.remove(wrapper);
        }
    }

    @Override
    public Boolean isBlackOrder(Long buyerUserId) {
        QueryWrapper<BlackRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("buyer_user_id",buyerUserId);
        return this.list(wrapper).size()>0;
    }
}
